// /Users/wanghongsheng/工作/miniprograms/ ShortVideo/components/full-scroll-view/full-scroll-view.js
Component({
  data: {
    height: 'auto',
    loading: false,
    refresh: false,
    topMsg: '',
    bottomMsg: ''
  },
  lifetimes: {
    attached: function () {
      this.initView();
      // 在组件实例进入页面节点树时执行
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  properties: {
    id: {
      type: String
    },
    api: {
      type: Function
    }
  },

  methods: {
    onRefresherrefresh() {
      this.setData({
        refresh: true
      }, () => {
        if (this.loading) {
          this.setData({
            refresh: false
          })
        } else {
          this.loadData(false)
        }
      });
    },
    async onReachBottom() {
      if (this.loading || this.data.bottomMsg == '已全部加载') return;
      this.setData({
        bottom: '加载中...'
      });
      this.loadData(true);
    },
    initView() {
      const query = this.createSelectorQuery();
      const res = query.select(".full-scroll-view");
      // let rect=res.boundingClientRect();

      const vm = this;
      res.boundingClientRect(function (rect) {
        try {
          const info = tt.getSystemInfoSync();
          const height = info.windowHeight - rect.top;
          vm.setData({
            height: `${height}px`
          })
        } catch {
          vm.setData({
            height: 'auto'
          })
        }
      })
      query.exec()
    },

    async loadData(isMore) {
      if (this.loading) return;
      // return
      this.loading = true;
      try {
        let [hasMore] = await this.api(isMore);
        this.setData({
          topMsg: "",
          bottomMsg: hasMore ? '' : '已全部加载',
          refresh: false
        })
      } catch {
        this.loading = false;
        this.setData({
          topMsg: '',
          bottomMsg: '',
          refresh: false
        })
      }
    }
  }
})